package cn.wzy.dao;

import cn.wzy.common.Constant;
import cn.wzy.common.PageInfo;
import cn.wzy.pojo.Customer;

import java.util.Date;
import java.util.Map;

/**
 * @author nihaowzy
 * @date 2020/12/24 11:18
 * 客户数据操作
 */
public class CustomerDao extends BaseDao {


    /**
     * 根据条件查询客户信息 如果没有条件就查询全部客户信息
     * @param params
     * @param page
     * @param limit
     * @return
     */
    public PageInfo<Customer> selectPage(Map<String ,String> params,Integer page ,Integer limit) throws Exception {
        String sql = "select  c.id, c.name ,c.sex ,c.phone ,c.company ,c.deleted ,c.user_id userId , c.create_time createTime ,c.update_time updateTime , c.delete_time deleteTime from customer c left join user u on c.user_id = u.id where  1 = 1";
        System.out.println(params.get("name"));
        if (params.containsKey("name")){
            sql += " and c.name like '%"+params.get("name")+"%' ";
        }
        if (params.containsKey("phone")){
            sql += " and c.phone = "+params.get("phone")+" ";
        }
        if (params.containsKey("sex")){
            sql += " and c.sex = "+params.get("sex")+" ";
        }
        if (params.containsKey("company")){
            sql += " and c.company like '%"+params.get("company")+"%' ";
        }
        if (params.containsKey("deleted")){
            sql += " and c.deleted = "+params.get("deleted")+" ";
        }
        if (params.containsKey("userId")){
            sql += " and c.user_id = "+params.get("userId")+" ";
        }

        PageInfo pageInfo = super.selectPage(sql, Customer.class, page, limit);
        return pageInfo;
    }

    /**
     * 根据姓名查询客户
     * @param phone
     * @return
     */
    public Customer selectByPhone (String phone) throws Exception {
        String sql = "select id from customer where phone = ?";
        return (Customer) super.selectOne(sql,Customer.class,phone);
    }

    /**
     * 添加客户信息
     * @param name
     * @param phone
     * @param sex
     * @param company
     * @param userId
     * @throws Exception
     */
    public void insertCustomer(String name , String phone , Integer sex , String company , Integer userId) throws Exception {
        String sql  = "insert into customer (name, phone, sex, company, deleted, user_id ) values (?,?,?,?,?,? )";
        super.update(sql,name,phone,sex , company , Constant.CUSTOMER_DELETE_NOT , userId);
    }

    /**
     * 更新客户信息
     * @param name
     * @param phone
     * @param sex
     * @param company
     * @param id
     * @throws Exception
     */
    public void updateCustomer(String name , String phone , Integer sex , String company ,Integer id ) throws Exception {
        String sql = "update customer set name = ? , sex = ? , phone = ? ,company = ? where id = ?";

        super.update(sql,name,sex,phone,company ,id);
    }

    /**
     * 根据id将客户的状态改为无效
     * @param deleted
     * @param id
     * @throws Exception
     */
     public void deleteCustomer(Integer deleted , Integer id) throws Exception {
        String sql = "update customer set deleted = ? ,delete_time = ? where id = ?";
        super.update(sql,deleted , new Date(),id);
     }

    /**
     * 批量修改客户的业务员
     * @param userId 业务员id
     * @param cIds 客户的id数组集合
     */
     public void updateSaleMan(String userId ,String...cIds) throws Exception {
         //组合sql
//         在指定 String 数组B的每个元素之间串联指定的分隔符 A，从而产生单个串联的字符串
//         string [] tmpStr={abc,def,ghi};
//         string jn = string.Join(“-“, tmpStr);
//         此时jn=”abc-def-ghi”;
         String customerIds = String.join(",", cIds);
         String sql = "update customer set user_id = ? where id in ( "+customerIds+" ) ";
         super.update(sql,userId);

     }
}
